/*
    SUSAN® - Sole of Unix Save ANything

   Copyright (C) 2007-2010 Hechzh Property
   Copyright (C) 2016-2020 Hechzh Property

   

   

   
*/
/*
 *  Kern Sibbald, October 2007
 */
/**
 * @file
 * Sample Plugin program
 */
#include "include/susan.h"
#include "dir_plugins.h"

#define PLUGIN_LICENSE "Susan AGPLv3"
#define PLUGIN_AUTHOR "Kern Sibbald"
#define PLUGIN_DATE "January 2008"
#define PLUGIN_VERSION "1"
#define PLUGIN_DESCRIPTION "Test Director Daemon Plugin"

namespace directordaemon {

/* Forward referenced functions */
static bRC newPlugin(PluginContext* ctx);
static bRC freePlugin(PluginContext* ctx);
static bRC getPluginValue(PluginContext* ctx, pDirVariable var, void* value);
static bRC setPluginValue(PluginContext* ctx, pDirVariable var, void* value);
static bRC handlePluginEvent(PluginContext* ctx, bDirEvent* event, void* value);


/* Pointers to Susan functions */
static bDirFuncs* susan_core_functions = NULL;
static bDirInfo* susan_plugin_interface_version = NULL;

static PluginInformation pluginInfo = {
    sizeof(pluginInfo), DIR_PLUGIN_INTERFACE_VERSION,
    DIR_PLUGIN_MAGIC,   PLUGIN_LICENSE,
    PLUGIN_AUTHOR,      PLUGIN_DATE,
    PLUGIN_VERSION,     PLUGIN_DESCRIPTION};

static PluginFunctions pluginFuncs = {
    sizeof(pluginFuncs), DIR_PLUGIN_INTERFACE_VERSION,

    /* Entry points into plugin */
    newPlugin,  /* new plugin instance */
    freePlugin, /* free plugin instance */
    getPluginValue, setPluginValue, handlePluginEvent};

#ifdef __cplusplus
extern "C" {
#endif

/**
 * loadPlugin() and unloadPlugin() are entry points that are
 *  exported, so Susan can directly call these two entry points
 *  they are common to all Susan plugins.
 *
 * External entry point called by Susan to "load" the plugin
 */
bRC loadPlugin(bDirInfo* lsusan_plugin_interface_version,
               bDirFuncs* lsusan_core_functions,
               PluginInformation** plugin_information,
               PluginFunctions** plugin_functions)
{
  susan_core_functions =
      lsusan_core_functions; /* set Susan funct pointers */
  susan_plugin_interface_version = lsusan_plugin_interface_version;
  printf("plugin: Loaded: size=%d version=%d\n", susan_core_functions->size,
         susan_core_functions->version);

  *plugin_information = &pluginInfo; /* return pointer to our info */
  *plugin_functions = &pluginFuncs;  /* return pointer to our functions */

  return bRC_OK;
}

/**
 * External entry point to unload the plugin
 */
bRC unloadPlugin()
{
  printf("plugin: Unloaded\n");
  return bRC_OK;
}

#ifdef __cplusplus
}
#endif

static bRC newPlugin(PluginContext* ctx)
{
  int JobId = 0;
  susan_core_functions->getSusanValue(ctx, bDirVarJobId, (void*)&JobId);
  printf("plugin: newPlugin JobId=%d\n", JobId);
  susan_core_functions->registerSusanEvents(ctx, 2, bDirEventJobStart,
                                              bDirEventJobEnd);
  return bRC_OK;
}

static bRC freePlugin(PluginContext* ctx)
{
  int JobId = 0;
  susan_core_functions->getSusanValue(ctx, bDirVarJobId, (void*)&JobId);
  printf("plugin: freePlugin JobId=%d\n", JobId);
  return bRC_OK;
}

static bRC getPluginValue(PluginContext* ctx, pDirVariable var, void* value)
{
  printf("plugin: getPluginValue var=%d\n", var);
  return bRC_OK;
}

static bRC setPluginValue(PluginContext* ctx, pDirVariable var, void* value)
{
  printf("plugin: setPluginValue var=%d\n", var);
  return bRC_OK;
}

static bRC handlePluginEvent(PluginContext* ctx, bDirEvent* event, void* value)
{
  char* name;
  int val;
  switch (event->eventType) {
    case bDirEventJobStart:
      printf("plugin: HandleEvent JobStart\n");
      break;
    case bDirEventJobEnd:
      printf("plugin: HandleEvent JobEnd\n");
      susan_core_functions->getSusanValue(ctx, bDirVarJob, (void*)&name);
      printf("plugin: bDirVarJob=%s\n", name);
      susan_core_functions->getSusanValue(ctx, bDirVarJobId, (void*)&val);
      printf("plugin: bDirVarJobId=%d\n", val);
      susan_core_functions->getSusanValue(ctx, bDirVarType, (void*)&val);
      printf("plugin: bDirVarType=%c\n", val);
      susan_core_functions->getSusanValue(ctx, bDirVarLevel, (void*)&val);
      printf("plugin: bDirVarLevel=%c\n", val);
      susan_core_functions->getSusanValue(ctx, bDirVarClient, (void*)&name);
      printf("plugin: bDirVarClient=%s\n", name);
      susan_core_functions->getSusanValue(ctx, bDirVarCatalog, (void*)&name);
      printf("plugin: bDirVarCatalog=%s\n", name);
      susan_core_functions->getSusanValue(ctx, bDirVarPool, (void*)&name);
      printf("plugin: bDirVarPool=%s\n", name);
      susan_core_functions->getSusanValue(ctx, bDirVarStorage, (void*)&name);
      printf("plugin: bDirVarStorage=%s\n", name);
      susan_core_functions->getSusanValue(ctx, bDirVarJobErrors, (void*)&val);
      printf("plugin: bDirVarJobErrors=%d\n", val);
      susan_core_functions->getSusanValue(ctx, bDirVarJobFiles, (void*)&val);
      printf("plugin: bDirVarJobFiles=%d\n", val);
      susan_core_functions->getSusanValue(ctx, bDirVarNumVols, (void*)&val);
      printf("plugin: bDirVarNumVols=%d\n", val);
      break;
  }
  susan_core_functions->getSusanValue(ctx, bDirVarJobName, (void*)&name);
  printf("Job Name=%s\n", name);
  susan_core_functions->JobMessage(ctx, __FILE__, __LINE__, M_INFO, 0,
                                    "JobMesssage message");
  susan_core_functions->DebugMessage(ctx, __FILE__, __LINE__, 1,
                                      "DebugMesssage message");
  return bRC_OK;
}
} /* namespace directordaemon */
